<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Menu_model extends CI_Model {

	public function __construct()
	{
           parent::__construct();
           $mongo = new MongoClient();
           $this->load->helper('url');
	}

        /*
         * Menu Functions
         */
        public function getmenu ($role = 'public', $page = 'home')
        {
	// Retrieve the menu items
               $result = $this->mongo_db
                          ->where_in('pages', array(
                            $page,
                            'all'  ))
                            ->where_in('roles', array(
                            $role,
                            'all'  ))  
                        ->get('menu');
                return $result ; 
        }
    
        public function setmenu()
        {
	// Get Menu Settings
        }
        
        public function menulist($type = 'all')
        {
            if ($type == 'all') {
               $result = $this->mongo_db->get('menu'); 
            } else {
//           $mongo = new MongoClient();
//           $mongodb = $mongo->areaforum;     
//           $crit = array('\$or' => array('name' => $type, 'parent' => $type));     
//	   $result = $mongodb->menu->find($crit);
//           var_dump($result);
//           $mongo->close;
             $result = $this->mongo_db->where(array('$or' => array(array('name' => $type), array('parent' => $type))))->get('menu');    
       }
                return $result ;  
        }
        
         public function menutoplevel()
        {
            $result = $this->mongo_db->where_in('parent', array( 'none' ))->get('menu');
            return $result ;  
        }
        
        public function getmenubyid($mid)
        {  // Returns a single menu item based on the mongoid
            $theuserId = new MongoId($mid); 
            $result = $this->mongo_db->where(array("_id" => $theuserId))->get('menu');
            // var_dump($result);
            return $result;
        }
        
        public function addpage2menu($id, $pagename, $section) {
            // Check that it is not already there
            if ($this->CheckMenuArrayForDuplicates($id, $pagename, 'pages') == FALSE ) {
           // $criteria = array('_id' => new MongoId($id)) ;
           // $find = $this->mongo_db->where($criteria)->where_in(array("pages" => $pagename))->get('menu');
            
                $result = $this->mongo_db->where( $criteria )->push(array('pages' => array( $pagename )))->update('menu'); 
            }
        }
        
        public function CheckMenuArrayForDuplicates($id, $name, $type) {
            // Check that it is not already there
            $criteria = array('_id' => new MongoId($id)) ;
            $find = $this->mongo_db->where($criteria)->where_in(array("pages" => $pagename))->get('menu');
            if (count($find) == 0) {
                return  FALSE; } else {  return  TRUE; }
        }
        
        public function updatemenu ($id, $data) {
            $criteria = array('_id' => new MongoId($id)) ;
            $result = $this->mongo_db->where( $criteria )->set($data)->update('menu'); 
        }
        
        public function insertmenu ($data) {
            $criteria = array('_id' => new MongoId($id)) ;
            $result = $this->mongo_db->insert('menu', $data); 
        }
}
?>
